package cn.tedu.charging.device.startup;


import cn.tedu.charging.device.dao.repository.GEOTemplateContext;
import cn.tedu.charging.device.dao.repository.StationCachRepository;
import cn.tedu.charging.device.dao.repository.StationCacheTemplateRepository;
import cn.tedu.charging.device.dao.repository.StationPORepository;
import cn.tedu.charging.device.pojo.po.StationPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@Component
public class StationDataInit implements ApplicationRunner {

    @Autowired
    private StationPORepository stationPORepository;

    @Autowired
    private StationCachRepository stationCachRepository;

    @Autowired
    private GEOTemplateContext geoTemplateContext;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("ApplicationRunner#run  init");
        log.debug("缓存预热 把数据库的数据缓存到Redis," +
                "在设备服务启动的时候,从数据库查询场站信息 位置信息(经纬度),基本信息(名称...),存入到Redis");
        log.debug("从数据库查询场站信息");

        List<StationPO> allStation = stationPORepository.getAllStation();
        log.debug("allStation= {}",allStation);
        StationCacheTemplateRepository stationCacheTemplateRepository = geoTemplateContext.geoRepositoryTemplate();

        if (!CollectionUtils.isEmpty(allStation)){
            stationCacheTemplateRepository.save(allStation);
        }


    }
}
